<template>
  <view class="content">
    <view class="btn bott-btn">
      <button class="new" type="primary" @click="handleAdd">+添加供应商</button>
    </view>
    <view class="content-box">
      <view class="item-box-ng" v-for="(item, index) in lists"
        :url="`/packageA/pages/common/adress-editing/index?id=${item.id}`">
        <view class="item-box" @click="handleDetail(item.id)">
          <text class="cph">{{ item.customerName }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import { getList } from '@/api/supplier';
  import { getDictList } from '@/api/login';
  export default {
    data() {
      return {
        proName: '',
        coalTypeList: [],
        lists: []
      };
    },
    async onShow() {
      this.lists = [];
      this.getSupplierList();
    },

    onPullDownRefresh() {
      this.lists = [];
      this.getSupplierList();
    },

    onReachBottom() {
      this.getSupplierList();
    },

    methods: {
      handleAdd() {
        uni.setStorageSync('title', '添加');
        uni.navigateTo({
          url: '/packageA/pages/common/supplier-editing/index'
        });
      },

      handleDetail(id) {
        uni.redirectTo({
          url: `/packageA/pages/supplier/details?id=${id}`
        });
      },

      linkToRecords(id) {
        uni.navigateTo({
          url: `/packageA/pages/product/records?id=${id}`
        });
      },

      confirmChange(data) {
        this.lists = [];
        this.getSupplierList();
      },

      async getDictList() {
        const res = await getDictList();
        if (res.code === 0) {
          this.coalTypeList = (res?.data || []).filter(
            (e) => e.dictType === 'coal_type'
          );
        }
      },

      //获取列表
      async getSupplierList() {
        try {
          const res = await getList();
          if (res.code == 0) {
            this.lists = res.data || [];
          }
          wx.stopPullDownRefresh();
        } catch (e) {
          return false;
        }
      },

      exchangeCoalTypeLabel(result) {
        return result.map((e) => {
          e.productType = this.coalTypeList.find(
            (m) => m.value === e.productType.toString()
          )?.label;
          return e;
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .content {
    height: 100vh;
    font-size: 24.88rpx;
    background-color: #e5e5e5;

    .content-box {
      padding: 20rpx 20rpx 124rpx 20rpx;

      .item-box-ng {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 20rpx;
        height: 124rpx;

        .item-box {
          height: 80rpx;
          line-height: 80rpx;
          box-sizing: content-box;
          padding: 20rpx;
          display: flex;
          justify-content: space-between;
          position: relative;
          width: 93%;
          background: #fff;
          border-radius: 16rpx;
          margin-bottom: 6rpx;

          .cph {
            flex: 1;
            font-weight: bold;
            font-size: 32rpx;
            padding-left: 20rpx;
          }
        }
      }
    }
  }
</style>